<?php

Yii::import('application.models._base.BaseEvent');

class Event extends BaseEvent
{
	public static function model($className=__CLASS__) {
		return parent::model($className);
	}

    /**
     * @param $event_type
     * @param $type
     * @param $from_user_id
     * @param $to_user_id
     * @param $vid
     * @param $title
     * @param string $vid1
     * @param string $title1
     *
     * Event::model()->addEvent(
        'event',
        'comment',
        $uid,
        $share_data->uid,
        $share_id,
        '评论'
        );
     */
    public function addEvent($event_type,$type,$from_user_id,$to_user_id,$vid,$title,$vid1='',$title1=''){
        $event_db = new Event();
        if($from_user_id != $to_user_id){
            $data = array(
                'event_type'=>$event_type,
                'type'=>$type,
                'from_user_id'=>$from_user_id,
                'to_user_id'=>$to_user_id,
                'is_read'=>0,
                'vid'=>$vid,
                'vid1'=>$vid1,
                'title'=>$title,
                'title1'=>$title1,
                'create_time'=>time(),
            );
            $event_db->setAttributes($data);
            $event_db->save();
        }
    }
    public function getCountEvent($uid){
        $count = $this->count('to_user_id=:to_user_id and is_read=:is_read',array(':to_user_id'=>$uid,':is_read'=>0));
        return $count;
    }
    public function getAjaxEvent($uid,$page){
        $num = Yii::app()->params['EVENT_PAGE'];
        $db = Yii::app()->db->createCommand();
        $p = ($page-1)*$num;
        $info = $db
            ->select('e.type,e.from_user_id,e.to_user_id,e.create_time,vid,vid1,title,title1,u.nick_name,u.domain,u.avatar_status,u.avatar_public')
            ->from('{{event}} e')
            ->leftJoin('{{user}} u','e.from_user_id = u.id')
            ->where('e.to_user_id=:to_user_id',array(':to_user_id'=>$uid))
            ->order('e.create_time desc')
            ->limit($num,$p)
            ->queryAll();
        return $info;
    }

    public function getEvent($uid){
        //先把所有的变为已读
        $ok = Event::model()->updateAll(array('is_read'=>1),'to_user_id=:to_user_id',array(':to_user_id'=>$uid));
        Yii::app()->user->setState('event',0);
        $num = Yii::app()->params['EVENT_PAGE'];
        $db = Yii::app()->db->createCommand();
        $info = $db
            ->select('e.type,e.from_user_id,e.to_user_id,e.create_time,vid,vid1,title,title1,u.nick_name,u.domain,u.avatar_status,u.avatar_public')
            ->from('{{event}} e')
            ->leftJoin('{{user}} u','e.from_user_id = u.id')
            ->where('e.to_user_id=:to_user_id',array(':to_user_id'=>$uid))
            ->order('e.create_time desc')
            ->limit($num)
            ->queryAll();
        return $info;
    }
    /**
     * 首页取得最新动态
     * @param $num显示数量
     */
    public function getVoice($num){

        $db = Yii::app()->db->createCommand();
        $info = $db
            ->select('e.type,e.from_user_id,e.to_user_id,e.vid,e.title,e.create_time,u.nick_name,u.domain,u.avatar_public,u.avatar_status')
            ->from('{{event}} e')
            ->leftJoin('{{user}} u','u.id=e.from_user_id')
            ->where('e.event_type=:event_type',array(':event_type'=>'event'))
            ->order('e.create_time desc')
            ->limit($num)
            ->queryAll();
        return $info;
    }

}